<?php
class SearchController extends BaseController{
    public function __construct($id = null){
        parent::__construct();
    }
    public function SearchNews($keyword) {
        //Tìm các tin bài dựa vào từ khóa keyword
        if(!$keyword){
            redirect('/home');
        }
        $db_count = new db_count('SELECT count(*) as count 
                                    FROM news 
                                    WHERE new_active = 1 
                                            AND new_date <= '.time().' 
                                            AND (MATCH(new_title,new_summary) AGAINST("'.$keyword.'" IN BOOLEAN MODE)
                                                OR new_tags LIKE "%'.$keyword.'%" OR new_title LIKE "%'.$keyword.'%")');
        $count_result = $db_count->total;unset($db_count);
        $db_query = new db_query('SELECT new_id,new_title,new_cat_id,new_date,new_hot,new_active,
                                         new_summary,new_picture,new_detail,new_relate,new_tags,new_type,
                                            MATCH(new_title) AGAINST("'.$keyword.'" IN BOOLEAN MODE) AS score 
                                    FROM news 
                                    LEFT JOIN categories_multi ON cat_id = new_cat_id 
                                    WHERE new_active = 1 
                                        AND new_date <= '.time().' 
                                        AND (MATCH(new_title,new_summary) AGAINST("'.$keyword.'" IN BOOLEAN MODE)
                                            OR new_tags LIKE "%'.$keyword.'%"  OR new_title LIKE "%'.$keyword.'%")
                                    ORDER BY score DESC, new_date DESC, new_id DESC 
                                    LIMIT 20');
        $list_news = array();
        while($row = mysql_fetch_assoc($db_query->result)) {
            $news = array();
            if($row['new_type'] == 'recruitment') {
                $news['link'] = generate_recruitment_detail_url(array('new_id'=>$row['new_id'],'new_title'=>$row['new_title']));
            }
            elseif($row['new_type'] == 'promotion') {
                $news['link'] = generate_promotion_detail_url(array('new_id'=>$row['new_id'],'new_title'=>$row['new_title']));
            }
            elseif($row['new_type'] == 'technology') {
                $news['link'] = generate_technology_detail_url(array('new_id'=>$row['new_id'],'new_title'=>$row['new_title']));
            }
            else {
                $news['link'] = generate_news_detail_url(array('new_id'=>$row['new_id'],'new_title'=>$row['new_title']));
            }
            $news['title'] = $row['new_title'];
            $news['picture'] = PATH_PICTURE_NEWS.$row['new_picture'];
            $news['summary'] = $row['new_summary'];
            $news['date'] = date('h:i a d-m-Y',$row['new_date']);
            $news['detail'] = $row['new_detail'];
            $news['relate'] = $row['new_relate'];
            $news['tags'] = $row['new_tags'];
            $list_news[] = $news;
        }
        unset($db_query);
        $html = SearchLayout::search_news(array('news'=>$list_news,'total'=>$count_result,'keyword'=>$keyword));
        return $html;
    }
    
    public function SearchProduct($keyword) {
        if(!$keyword){
            redirect('/home');
        }
        $db_count = new db_count('SELECT count(*) as count 
                                    FROM products 
                                    WHERE pro_active = 1 
                                            AND pro_date <= '.time().' 
                                            AND (MATCH(pro_name,pro_code_number,pro_model_number) AGAINST("'.$keyword.'" IN BOOLEAN MODE)
                                                OR pro_name LIKE "%'.$keyword.'%")');
        $count_result = $db_count->total;unset($db_count);
        $db_query = new db_query('SELECT pro_id, pro_name,pro_price,pro_code_number,pro_model_number,
                                         pro_thumb, pro_promotion,
                                            MATCH(pro_name,pro_code_number,pro_model_number) AGAINST("'.$keyword.'" IN BOOLEAN MODE) AS score 
                                    FROM products 
                                    WHERE pro_active = 1 
                                        AND pro_date <= '.time().' 
                                        AND (MATCH(pro_name,pro_code_number,pro_model_number) AGAINST("'.$keyword.'" IN BOOLEAN MODE)
                                            OR pro_name LIKE "%'.$keyword.'%")
                                    ORDER BY score DESC, pro_date DESC, pro_id DESC 
                                    LIMIT 20');
        $list_pro = array();
        while($row = mysql_fetch_assoc($db_query->result)){
            $pro = array();
            $pro['link'] = generate_product_detail_url(array('pro_id'=>$row['pro_id'],'pro_name'=>$row['pro_name']));
            $pro['thumb'] = PATH_PICTURE_PRODUCT . $row['pro_thumb'];
            $pro['hot'] = 1;
            $pro['new'] = 1;
            $pro['price'] = $row['pro_price'];
            $pro['gift'] = $row['pro_promotion'];
            $pro['name'] = $row['pro_name'];
            $list_pro[] = $pro;
        }unset($db_query);
        //Lay ra hang san xuat
        $db_query = new db_query('SELECT fac_id,fac_name FROM factory');
        $list_factory = array();
        while($row = mysql_fetch_assoc($db_query->result)){
            $fac = array();
            $fac['link'] = generate_factory_url(array('fac_id'=>$row['fac_id'],'fac_name'=>$row['fac_name']));
            $fac['name'] = $row['fac_name'];
            $list_factory[] = $fac;
        }
        $html = SearchLayout::search_product(array('pro'=>$list_pro,'factory'=>$list_factory,'total'=>$count_result,'keyword'=>$keyword));
        return $html;
    }
}

?>